package com.wang.learn.cloudredis.utils;

/** 抢单测试
 * @author Wang
 * @description
 * @create 2021-06-30 16:19
 */

import org.apache.http.client.ClientProtocolException;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class ConcurrentTest implements Runnable {
    public static CountDownLatch cdl = new CountDownLatch(1);

    @Override
    public void run() {
        try {
            cdl.await();
            long start = System.currentTimeMillis();
            String url = "http://localhost:8080/redis/setIfAbsent";
            Map<String, Object> map = new HashMap<>();
            map.put("token","2b3160d8-6b50-4814-8cf7-3acbb614547c");
            String s = HttpUtils.postJson(url, map, "");
            System.out.println(s);
            if(s.contains("Successful")){
                System.out.println("耗时：" + (System.currentTimeMillis() - start));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


class CatchTicket {
    public static void main(String[] args) {

        ConcurrentTest p = new ConcurrentTest();
        for (int i = 0; i < 100; i++) {
            new Thread(p).start();
        }
        ConcurrentTest.cdl.countDown();
    }
}
